/* Standard libraries */
#include <stdio.h>

/* Own includes */
#include "../include/global.h"
#include "../include/output.h"
#include "../include/mainmenu.h"
#include "../include/maingame.h"

/* Defines */
#define MENU_STATE_MAIN 0
#define MENU_STATE_LOAD 1
#define MENU_STATE_OPTIONS 2
#define MENU_SEL_NEWGAME 0
#define MENU_SEL_LOADGAME 1
#define MENU_SEL_OPTIONS 2
#define MENU_SEL_QUIT 3

/* Internal prototypes */
void draw_menu(int msta, int msel);
int menu_input(int *msta, int *msel, int *mopt);
void show_credits();
void load_menu();
void free_menu();

void main_menu()
{
	int msta = 0;
	int msel = 0;
	int mopt = 3;

	load_menu();

	/* Draw background & options to screen */
    draw_menu(msta, msel);

    /*
     *Draw the screen once,
     *so we can see the options before pressing a key
     */
	sync();


	while(!mm_done)
	{
		if(menu_input(&msta, &msel, &mopt) == 1)
		{
			draw_menu(msta, msel);
			sync();
		} else rest(1);	/* don't hog the CPU */
	}

	/* Show credits upon quitting */
    if(mm_done == 2) show_credits();

	free_menu();
}

void draw_menu(int msta, int msel)
{
    blit(mm_bg, buffer, 0, 0, 0, 0, mm_bg->w, mm_bg->h);

	switch(msta)
	{
		case MENU_STATE_MAIN:
		{
			text_center(screen->w / 2, 5, 255, 0, 0, "Random RPG Game 1.0");

            text_center(screen->w / 2, 152, (msel == 0 ? 255 : 50), 255, 50, "New Game");

            text_center(screen->w / 2, 164, (msel == 1 ? 255 : 50), 255, 50, "Load Game");

            text_center(screen->w / 2, 176, (msel == 2 ? 255 : 50), 255, 50, "Options");

            text_center(screen->w / 2, 188, (msel == 3 ? 255 : 50), 255, 50, "Exit");
		} break;
		case MENU_STATE_OPTIONS:
		{
		    text_center(screen->w / 2, 5, 255, 0, 0, "Random RPG Game 1.0");

		    text_center(screen->w / 2, 152, (msel == 0 ? 255 : 50), 255, 50, "Resolution: 320x200");

		    text_center(screen->w / 2, 164, (msel == 1 ? 255 : 50), 255, 50, "Back");
		} break;
	}
}

int menu_input(int *msta, int *msel, int *mopt)
{
	if(keypressed())
	{
		int val = readkey() >> 8;

		switch(val)
		{
			case KEY_UP:
			{
				if(--*msel < 0) *msel = *mopt;
				break;
			}
			case KEY_DOWN:
			{
				if(++*msel > *mopt) *msel = 0;
				break;
			}
			case KEY_ENTER:
			{
			    switch(*msta)
			    {
                    case MENU_STATE_MAIN:
                    {
                        if(*msel == MENU_SEL_NEWGAME)
                        {
                            /* Create new character */
                            /* Play intro */
                            game_run();
                        }
                        if(*msel == MENU_SEL_OPTIONS)
                        {
                            *msel = 0;
                            *mopt = 1;
                            *msta = MENU_STATE_OPTIONS;
                        }
                        if(*msel == MENU_SEL_QUIT)
                        {
                            mm_done = 1;
                        }
                    } break;
                    case MENU_STATE_OPTIONS:
                    {
                        if(*msel == 1)
                        {
                            *msel = 2;
                            *mopt = 3;
                            *msta = MENU_STATE_MAIN;
                        }
                    } break;
			    }
			}
		}
		clear_keybuf();	/* Clear keybuffer */
		return 1; /* Redraw */
	}

	return 0; /* Do not redraw */
}

void show_credits()
{
    BITMAP * mcbg = load_bitmap("graphics/mainmenu/credits.png", 0);

	if(!mcbg)
	{
		printf("ERROR: Could not load credits background.\nTerminating.\n");
		return;
	}

	int ypos = screen->h + 30;
	blit(mcbg, buffer, 0, 0, 0, 0, mcbg->w, mcbg->h);

	ticks = 0;

	while(!key[KEY_ESC])
	{
		while(ticks > 0)
		{
			ypos--;
			ticks--;
		}
		blit(mcbg, buffer, 0, 0, 0, 0, mcbg->w, mcbg->h);

        text_center(screen->w / 2, ypos, 255, 150, 50, "Random RPG Game");

        text_center(screen->w / 2, ypos + 30, 255, 255, 0, "Written by");
        text_center(screen->w / 2, ypos + 40, 255, 255, 0, "Major tom");

        text_center(screen->w / 2, ypos + 70, 255, 255, 0, "Programming By");
        text_center(screen->w / 2, ypos + 80, 255, 255, 0, "Nikolai Ugelvik");

        text_center(screen->w / 2, ypos + 110, 255, 255, 0, "Graphics by");
        text_center(screen->w / 2, ypos + 120, 255, 255, 0, "John the fisherman");

		sync();

		if(ypos < -150) {
		    destroy_bitmap(mcbg);
			break;
		}
	}
	/* Make sure to clean up if the user presses ESC aswell */
	destroy_bitmap(mcbg);
}

void load_menu()
{
	mm_bg = load_bitmap("graphics/mainmenu/mainmenu.png", 0);

	if(!mm_bg)
	{
		printf("ERROR: Could not load menu background.\nTerminating.\n");
		mm_done = 1;
	}
}

void free_menu()
{
	destroy_bitmap(mm_bg);
}
